import { http } from "@/utils/http";
import { baseUrlApi } from "./utils";
import axios from "axios";

// 数据来源
export const codeList = data => {
  return http.get(baseUrlApi("/code/list"), { data });
};

export const fetchFileList = params => {
  console.log("Sending request with params:", params);

  return axios.post(
    baseUrlApi("/code/findWithValidation"),
    params // 不手动序列化
  );
};

// 从数据湖下载文件接口
export const downloadFileFromDataLake = data => {
  // data 应该是一个对象，包含 { path, type }
  return axios
    .post(baseUrlApi("/code/download"), data, {
      responseType: "blob", // 指定响应类型为 blob
      headers: {
        "Content-Type": "application/json" // 确保设置 Content-Type
      }
    })
    .then(response => {
      // 处理响应，创建 File 对象
      const extractFileName = path => {
        return path.substring(path.lastIndexOf("/") + 1);
      };
      const fileName = extractFileName(data.path);
      const file = new File([response.data], fileName, {
        type: response.headers["content-type"] || "application/octet-stream"
      });
      return file; // 返回文件对象
    })
    .catch(error => {
      console.error("下载文件失败:", error);
      throw error; // 将错误抛出，以便调用者处理
    });
};

//选择文件
export const fetchFileList2 = (path, limit = 10) => {
  return http.post(
    baseUrlApi("/code/queryData"),
    {
      path,
      limit
    },
    {
      headers: {
        "Content-Type": "application/json" // 确保设置 Content-Type
      }
    }
  );
};

// 关键字搜索获取表名

// 数据清洗接口
export const codeClean = data => {
  return http.post(
    baseUrlApi("/code/clean"),
    { data },
    {
      headers: {
        "Content-Type": "multipart/form-data"
      }
    }
  );
};

// 数据降维接口
export const codeDimreduction = data => {
  return http.post(
    baseUrlApi("/code/dimreduction"),
    { data },
    {
      headers: {
        "Content-Type": "multipart/form-data"
      }
    }
  );
};

// 数据融合接口
export const codeFusion = data => {
  return http.post(
    baseUrlApi("/code/fusion"),
    { data },
    {
      headers: {
        "Content-Type": "multipart/form-data"
      }
    }
  );
};

// 状态估计接口
export const codeEstimation = data => {
  return http.post(
    baseUrlApi("/code/estimation"),
    { data },
    {
      headers: {
        "Content-Type": "multipart/form-data"
      }
    }
  );
};
